package com.sx.manage.service.profit;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.NumberUtil;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.service.company.IMBaseCompanyService;
import com.sx.manage.service.goods.IMGoodsInfoService;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.form.consumer.MCompanyForm;
import com.sx.model.manage.mybatis.form.profit.MProfitForm;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.profit.MProfitVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.shopping.mybatis.vo.goods.MGoodsInfoVO;

/**
 * 返利规则设置web service
 * 
 * @author Administrator
 *
 */
@Service("companyprofitWebService")
public class MCompanyprofitWebService {
	/*** 日志 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	// 公司返利
	@Resource(name = "companyprofitServiceImpl")
	private IMCompanyprofitService companyprofitServiceImpl;
	// 公司信息
	@Resource(name = "mBaseCompanyServiceImpl")
	private IMBaseCompanyService mBaseCompanyServiceImpl;
	// 渠道返利
	@Resource(name = "channelprofitserviceimpl")
	private IMChannelprofitService channelprofitserviceimpl;
	// 渠道客户返利
	@Resource(name = "channelCompanyprofitServiceImpl")
	private IMChannelCompanyprofitService channelCompanyprofitServiceImpl;
	// 返利规则商品
	@Resource(name = "goodsInfoServiceImpl")
	private IMGoodsInfoService goodsInfoServiceImpl;

	public Logger get_log() {
		return _log;
	}

	public void set_log(Logger _log) {
		this._log = _log;
	}

	public IMBaseCompanyService getmBaseCompanyServiceImpl() {
		return mBaseCompanyServiceImpl;
	}

	public void setmBaseCompanyServiceImpl(IMBaseCompanyService mBaseCompanyServiceImpl) {
		this.mBaseCompanyServiceImpl = mBaseCompanyServiceImpl;
	}

	public IMChannelprofitService getChannelprofitserviceimpl() {
		return channelprofitserviceimpl;
	}

	public void setChannelprofitserviceimpl(IMChannelprofitService channelprofitserviceimpl) {
		this.channelprofitserviceimpl = channelprofitserviceimpl;
	}

	public IMChannelCompanyprofitService getChannelCompanyprofitServiceImpl() {
		return channelCompanyprofitServiceImpl;
	}

	public void setChannelCompanyprofitServiceImpl(IMChannelCompanyprofitService channelCompanyprofitServiceImpl) {
		this.channelCompanyprofitServiceImpl = channelCompanyprofitServiceImpl;
	}

	public IMGoodsInfoService getGoodsInfoServiceImpl() {
		return goodsInfoServiceImpl;
	}

	public void setGoodsInfoServiceImpl(IMGoodsInfoService goodsInfoServiceImpl) {
		this.goodsInfoServiceImpl = goodsInfoServiceImpl;
	}

	public IMCompanyprofitService getCompanyprofitServiceImpl() {
		return companyprofitServiceImpl;
	}

	public void setCompanyprofitServiceImpl(IMCompanyprofitService companyprofitServiceImpl) {
		this.companyprofitServiceImpl = companyprofitServiceImpl;
	}

	/** 1 增加数据 */
	public ResultBooleanVO save(MProfitForm form, MConsumerLoginVO loginConsumer) {
		if (StringUtils.isBlank(form.getCompany_type())) {
			_log.debug(" 参数 company_type 为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		_log.debug("参数form=" + JackJsonUtil.objToJson(form));

		ResultBooleanVO result = null;
		if (MSystemConstants.CHANNEL.equals(form.getCompany_type())) {
			if (null != form.getChannel_id()) {
				return new ResultBooleanVO(false, null, "渠道不需要配置所属渠道");
			}
			result = channelprofitserviceimpl.save(form, loginConsumer);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(form.getCompany_type())) {
			if (null != form.getChannel_id()) {
				return new ResultBooleanVO(false, null, "直销客户不需要配置所属渠道");
			}
			result = this.companyprofitServiceImpl.save(form, loginConsumer);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(form.getCompany_type())) {
			if (null == form.getChannel_id()) {
				return new ResultBooleanVO(false, null, "渠道客户所属渠道不能为空");
			}
			result = this.channelCompanyprofitServiceImpl.save(form, loginConsumer);
		}
		return result;
	}

	/** 2 删除数据 * @param form */
	public ResultBooleanVO deleteById(Long[] ids, String company_type, MConsumerLoginVO loginConsumer) {
		_log.debug("删除配置规则配置");
		if (StringUtils.isBlank(company_type)) {
			_log.debug(" 参数 company_type 为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		ResultBooleanVO result = null;
		if (MSystemConstants.CHANNEL.equals(company_type)) {
			result = this.channelprofitserviceimpl.deleteById(ids, loginConsumer);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(company_type)) {
			result = this.companyprofitServiceImpl.deleteById(ids, loginConsumer);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(company_type)) {
			result = this.channelCompanyprofitServiceImpl.deleteById(ids, loginConsumer);
		}
		return result;
	}

	/** 4 * 根据id查询用户 */
	public ResultObjectVO<MProfitVO> queryById(Long id, String company_type) {
		_log.debug("根据id查询数据id=" + id + ",company_type=" + company_type);
		if (StringUtils.isBlank(company_type)) {
			_log.debug(" 参数 company_type 为空");
			return new ResultObjectVO<>("m.parameter.is.null");
		}
		ResultObjectVO<MProfitVO> result = null;
		if (MSystemConstants.CHANNEL.equals(company_type)) {
			result = this.channelprofitserviceimpl.queryById(id);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(company_type)) {
			result = this.companyprofitServiceImpl.queryById(id);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(company_type)) {
			result = this.channelCompanyprofitServiceImpl.queryById(id);
		}
		result.getObjVo().setCompany_type(company_type);
		searchCompany(result.getObjVo());
		return result;
	}

	/** 5 * 查询分页 */
	public ResultListVO<MProfitVO> queryPage(MProfitForm form) {
		_log.debug("webservice查询分页信息");
		if (StringUtils.isBlank(form.getCompany_type())) {
			_log.debug(" 参数 company_type 为空");
			return new ResultListVO<>("m.parameter.is.null");
		}
		ResultListVO<MProfitVO> result = null;
		if (MSystemConstants.CHANNEL.equals(form.getCompany_type())) {
			result = this.channelprofitserviceimpl.queryPage(form);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(form.getCompany_type())) {
			result = this.companyprofitServiceImpl.queryPage(form);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(form.getCompany_type())) {
			result = this.channelCompanyprofitServiceImpl.queryPage(form);
		}

		_log.debug("webservice查询公司信息");
		searchCompany(result.getData());
		return result;
	}

	/** 6 * 根据ids 更改数据状态 */
	public ResultBooleanVO editStatus(String company_type, MDataStatusType data_status, Long[] ids, MConsumerLoginVO loginConsumer) {
		_log.debug("修改配置规则显示状态data_status=" + data_status.toString() + ",ids=" + NumberUtil.arrayToStr(ids));
		if (StringUtils.isBlank(company_type)) {
			_log.debug(" 参数 company_type 为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		ResultBooleanVO result = null;
		if (MSystemConstants.CHANNEL.equals(company_type)) {
			result = this.channelprofitserviceimpl.editStatus(data_status, ids, loginConsumer);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(company_type)) {
			result = this.companyprofitServiceImpl.editStatus(data_status, ids, loginConsumer);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(company_type)) {
			result = this.channelCompanyprofitServiceImpl.editStatus(data_status, ids, loginConsumer);
		}
		return result;
	}

	/** 6.1 显示 */
	public ResultBooleanVO isShow(String company_type, Long id, MConsumerLoginVO loginConsumer) {
		_log.debug("设为显示 数据 id=" + id);
		if (StringUtils.isBlank(company_type)) {
			_log.debug(" 参数 company_type 为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		ResultBooleanVO result = null;
		if (MSystemConstants.CHANNEL.equals(company_type)) {
			result = this.channelprofitserviceimpl.editShow(Long.valueOf(MSystemConstants.NO_), id, loginConsumer);
		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(company_type)) {
			result = this.companyprofitServiceImpl.editShow(Long.valueOf(MSystemConstants.NO_), id, loginConsumer);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(company_type)) {
			result = this.channelCompanyprofitServiceImpl.editShow(Long.valueOf(MSystemConstants.NO_), id, loginConsumer);
		}
		return result;
	}

	/** 6.2 不显示 */
	public ResultBooleanVO isNotShow(String company_type, Long id, MConsumerLoginVO loginConsumer) {
		_log.debug("设为不显示 数据 id=" + id);
		if (StringUtils.isBlank(company_type)) {
			_log.debug(" 参数 company_type 为空");
			return new ResultBooleanVO("m.parameter.is.null");
		}
		ResultBooleanVO result = null;
		if (MSystemConstants.CHANNEL.equals(company_type)) {
			result = this.channelprofitserviceimpl.editShow(Long.valueOf(MSystemConstants.YES_), id, loginConsumer);

		} else if (MSystemConstants.DIRECT_CONSUMER_.equals(company_type)) {
			result = this.companyprofitServiceImpl.editShow(Long.valueOf(MSystemConstants.YES_), id, loginConsumer);
		} else if (MSystemConstants.CHANNEL_CONSUMER_.equals(company_type)) {
			result = this.channelCompanyprofitServiceImpl.editShow(Long.valueOf(MSystemConstants.YES_), id, loginConsumer);
		}
		return result;
	}

	/** 配置集合查询公司 **/
	private void searchCompany(List<MProfitVO> voList) {
		if (null != voList && !voList.isEmpty()) {
			for (MProfitVO vo : voList) {
				searchCompany(vo);
			}
		}

	}

	/** 单个配置查询公司 **/
	private void searchCompany(MProfitVO vo) {
		if (null != vo) {
			ResultObjectVO<MCompanyVO> voResult = null;
			MCompanyForm form = new MCompanyForm();
			if(null!=vo.getCompany_id()) {
				form.setId(vo.getCompany_id());
				voResult = mBaseCompanyServiceImpl.search_byIds(form);
				if (voResult.isSuccess()) {
					vo.setCompany_name(voResult.getObjVo().getCompany_name());
					vo.setCompanyVO(voResult.getObjVo());
				}
			}
			if (StringUtils.isNotBlank(vo.getProduct_id())) {
				ResultObjectVO<MGoodsInfoVO> goodsResult = goodsInfoServiceImpl.queryById(Long.valueOf(vo.getProduct_id()));
				if (goodsResult.isSuccess() && null != goodsResult.getObjVo()) {
					vo.setProduct_name(goodsResult.getObjVo().getName());
				}
			}
			if(null!=vo.getChannel_id()) {
				form.setId(vo.getChannel_id());
				voResult = mBaseCompanyServiceImpl.search_byIds(form);
				if (voResult.isSuccess()) {
					vo.setChannel_name(voResult.getObjVo().getCompany_name());
				}
			}
		}
	}

}
